<?php

class tutorial extends Model
{
	
	function getTutorialById($id)
	{
		$gtutorial = $this->db->getAll("SELECT tutorial.file File,
				tutorial.description, 
				tutorial.date,
				tutorial.title,
				tutorial.topic_id,
				organization.name 
			FROM tutorial,organization 
			WHERE tutorial.org_id = organization.org_id 
			AND tutorial.topic_id = ?
			ORDER BY tutorial.date DESC",
			array( (int) $id)	
		);
		return $gtutorial;
	}

	function getTutorial($orgname)
	{
		$gtutorial = $this->db->getAll("SELECT tutorial.file File,
				tutorial.description, 
				tutorial.date,
				tutorial.topic_id,
				tutorial.title,
				organization.name
			FROM tutorial,organization 
			WHERE tutorial.org_id = organization.org_id 
			AND organization.name = ?
			ORDER BY tutorial.date DESC",
			array($orgname)	
		);
		return $gtutorial;
	}

	function insertTutorial( $org_id, $file, $title, $date, $desc )
	{
		$this->db->startTrans();
		$itutorial = $this->db->execute("INSERT INTO tutorial(org_id,
			file,
			title,
			date,
			description)
			VALUES ( ?, ?, ?, ?, ? )", 
			array( (int) $org_id, $file, $title, $date, $desc )
		);
		return $this->db->lastInsertId();
	}
	
	function insertTutorialFail() {
		$this->db->rollBack();
		return false;
	}
	
	function insertTutorialDone() {
		$this->db->commit();
		return true;
	}

	function updateTutorial( $org_id, $file, $date, $desc, $topic_id ) 
	{
		$ututorial = $this->db->execute("UPDATE tutorial
			SET org_id = ?,
				file = ?,
				date = ?,
				description = ?
			WHERE topic_id = ?",
			array( (int) $org_id, $file, $date, $desc, (int) $topic_id )
		);
		return $ututorial;
	}

	function deleteTutorial ( $topic_id )
	{
		$dtutorial = $this->db->execute("DELETE FROM tutorial
			WHERE topic_id = ? ",
			array( $topic_id )
		);
	}

		function getComment($id)
	{
		$gcomment = $this->db->getAll("SELECT tutorial.title,
				ct.stud_no, 
				ct.date,
				ct.comment,
				organization.name Org,
				user.lastname, 
				user.firstname,
				user.middlename
			FROM tutorial,organization, commenttutorial ct, user
			WHERE tutorial.org_id = organization.org_id 
			AND ct.stud_no = user.stud_no
			AND ct.topic_id = tutorial.topic_id
			AND tutorial.topic_id = ?
			ORDER BY ct.date DESC",
			array($id));
		return $gcomment;
	}

	function insertComment ( $topic_id, $stud_no, $date, $comment )
	{
		$icomment = $this->db->execute("INSERT INTO commenttutorial( topic_id,
			stud_no,
			date,
			comment )
			VALUES ( ?, ?, ?, ? )", 
			array( (int) $topic_id, $stud_no, $date, $comment )
		);
		return $icomment;
	}

	function updateComment( $article_id, $stud_no, $date, $comment, $comment_id ) 
	{
		$ucomment = $this->db->execute("UPDATE commenttutorial
			SET topic_id = ?,
			stud_no = ?,
			date = ?,
			comment = ?			
			WHERE comment_id = ?",
			array( (int) $article_id, $stud_no, $date, $comment, (int) $comment_id  )
		);
		return $ucomment;
	}

	function deleteComment ( $comment_id )
	{
		$dphoto = $this->db->execute(
			"DELETE FROM commenttutorial where comment_id = ?",
			array( $comment_id  ) 
		);
	}
	
}
